package cn.wanho.controller;

import cn.wanho.rvo.ResultVO;
import cn.wanho.service.exam.ExamService;
import cn.wanho.service.exam.ExamSheetExcel;
import cn.wanho.service.exam.StudentService;
import cn.wanho.service.user.UserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    @Resource
    private ExamService examService;

    @Resource
    private ExamSheetExcel examSheetExcel;

    /**
     * @author jingxu
     * @date 2022-8-5
     * 学生参加考试
     */
    @GetMapping("/takeAnExamination")
    public ResultVO takeAnExamination(Integer paperId) {
        return examService.takeExamination(paperId);
    }

    /**
     * @author jingxu
     * @date 2022-8-7
     * 学生提交试卷
     */
    @PostMapping("/commit")
    public ResultVO commit(/*@RequestPart("file") MultipartFile file, */HttpServletRequest request) throws IOException {
        MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class);
        MultipartFile file = multipartRequest.getFile("file");
        Integer userId = (Integer) request.getAttribute("userId");
        //int studentId = Integer.parseInt(userId);
        return examSheetExcel.importExcel(file, userId);
    }

    /**
     * @author jingxu
     * @date 2022-8-7
     * 学生查询所有成绩
     */
    @GetMapping("/queryExamContent")
    public ResultVO queryExamContent(Integer paperId, HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("userId");
        //int studentId = Integer.parseInt(userId);
        return examService.queryExamContentScore(paperId, userId);
    }




    /**
     * 查询所有学生各个阶段的总成绩
     *
     * @param request
     * @return
     */
    @GetMapping("/queryAllScore")
    public ResultVO queryAllScore(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("userId");
        return studentService.queryStudentAllScore(userId);
    }
    @GetMapping("/queryPaper")
    public ResultVO queryPaper(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("userId");
        return studentService.queryPaper(userId);
    }

    @PostMapping("/reconsider")
    public ResultVO studentApplyFor(Long subjectId , HttpServletRequest request){
        Integer userId = (Integer) request.getAttribute("userId");
        return studentService.studentApplyFor(subjectId,userId);
    }

}
